<template>
  <div class="app-container">
    <el-form :inline="true"  class="demo-form-inline">
      <el-form-item label="讲师名">
        <el-input v-model="teacherQuery.name" ></el-input>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacherQuery.level" >
          <el-option label="高级讲师" value="1"></el-option>
          <el-option label="首席讲师" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="添加时间">
      <el-date-picker
        v-model="teacherQuery.begin"
        type="datetime"
        placeholder="选择开始时间"
        value-format="yyyy-MM-dd HH:mm:ss"
        default-time="00:00:00"/>
      </el-form-item>

      <el-form-item>
        <el-date-picker v-model="teacherQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <template>
      <el-table  :data="list" element-loading-text="数据加载中" border fit highlight-current-row >
        <el-table-column label="序号" width="70" align="center">
          <template slot-scope="scope">
            {{ (page - 1) * limit + scope.$index + 1 }}
          </template>
        </el-table-column>

        <el-table-column prop="avatar" label="头像"  width="100">
          <template slot-scope="scope">
            <img :src="scope.row.avatar" min-width="70" height="70"/>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="80"></el-table-column>
        <el-table-column prop="intro" label="简介" ></el-table-column>
        <el-table-column prop="career" label="资历" width="130"></el-table-column>

        <el-table-column prop="lever" label="头衔" width="130">
          <template slot-scope="scope">
            {{scope.row.lever==1?'高级讲师':'首席讲师'}}
          </template>
        </el-table-column>

        <el-table-column prop="gmtCreate" label="加入时间" width="200"></el-table-column>

        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
<!--          <router-link :to="'/teacher/edit/'+scope.row.id">   &lt;!&ndash;通过路由回显&ndash;&gt;-->
<!--            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>-->
<!--          </router-link>-->
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="getInfo(scope.row.id)">编辑</el-button>
            <el-dialog title="用户数据修改" :visible.sync="dialogFormVisible">
              <el-form label-width="80px">
                <el-form-item label="讲师id">
                  <el-input v-model="teacher.id" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="讲师名称">
                  <el-input v-model="teacher.name"></el-input>
                </el-form-item>
                <el-form-item label="讲师排序">
                  <el-input v-model="teacher.sort" controls-position="right" min="0"></el-input>
                </el-form-item>
                <el-form-item label="讲师头衔">
                  <el-select v-model="teacher.level" clearable placeholder="请选择">
                    <!--
                      数据类型一定要和取出的json中的一致，否则没法回填
                      因此，这里value使用动态绑定的值，保证其数据类型是number
                    -->
                    <el-option :value="1" label="高级讲师"/>
                    <el-option :value="2" label="首席讲师"/>
                  </el-select>
                </el-form-item>
                <el-form-item label="讲师资历">
                  <el-input v-model="teacher.career"/>
                </el-form-item>
                <el-form-item label="讲师简介">
                  <el-input v-model="teacher.intro" :rows="10" type="textarea"/>
                </el-form-item>
                <!-- 讲师头像：TODO -->
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="updateTeacher">确 定</el-button>
              </div>
            </el-dialog>

          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-size="limit"
        :total="total"
        style="padding: 30px 0; text-align: center;"
        layout="total, prev, pager, next, jumper"
        @current-change="getList"
      />
    </template>
  </div>
</template>

<script>
// 引入teacher.js文件
import teacher from '@/api/edu/teacher'

export default {
  // 写核心代码位置
  data() { // 定义变量和初始值
    return {
      dialogFormVisible:false,
      list: null,
      page: 1, // 当前页
      limit: 10, // 每页条数
      total: 123, // 总记录数
      teacherQuery: {
      } // 查询条件对象
      ,teacher: {
        name: '',
        sort: 0,
        level: 1,
        career: '',
        intro: '',
        avatar: ''
      },
    }
  },
  created() { // 页面渲染执行之前 一般调用methods定义的方法
    this.getList()
  },
  methods: {
    // 讲师列表的方法
    getList(page=1) {
      this.page=page
      teacher.getTeacherListPage(this.page, this.limit, this.teacherQuery)
        .then(response => {
          // response接口返回数据
          // console.log(response)
          this.list = response.data.rows
          this.total = response.data.total
        })
        .catch(error => {
          console.log(error)
        })
    },
    removeDataById(id){
        //调用删除方法
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          teacher.removeDataById(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          //回到列表页面
          this.getList()
        })
    },
    resetData(){ //清空的方法
      this.teacherQuery={}
      this.getList(id)
    },
    //根据讲师id查询方法
    getInfo(id){
      this.dialogFormVisible=true
      teacher.getTeacherInfo(id)
        .then(response=>{
          this.teacher=response.data.teacher
        })
    },
    //修改讲师的方法
    updateTeacher(){
      this.dialogFormVisible=false
      teacher.updateTeacher(this.teacher)
        .then(response => { //添加成功
        //提示信息
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
      this.$router.go(0)
    })
  }
  }
}
</script>

<style scoped>

</style>
